﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class product_sort_edit : System.Web.UI.Page
{
    BLL.Business.BookBLL bookBLL = new BLL.Business.BookBLL();
    Model.Book book = new Model.Book();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            DataBindBig();
            DataBindSmall();
            DataBindDropList();
        }
    }

    #region MuiltyView View设置
    protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
    {
        mVType.ActiveViewIndex = Int32.Parse(e.Item.Value);
    }
    //点击产品介绍时的css设置
    protected void lButtonBig_Click(object sender, EventArgs e)
    {
        mVType.ActiveViewIndex = 0;
        Cell1.Attributes["class"] = "SelectedTopBorder";
        Cell2.Attributes["class"] = "TopBorder";
    }
　　
    protected void lButtonSmall_Click(object sender, EventArgs e)
　　{
        mVType.ActiveViewIndex = 1;
　　　　Cell1.Attributes["class"]="TopBorder";
　　　　Cell2.Attributes["class"]="SelectedTopBorder";
  }
    #endregion 

    #region 数据源绑定
    public void DataBindBig()
    {
        DataSet ds = new DataSet();
        ds = bookBLL.SelectBigType();
        if ((ds == null) ||
            (ds.Tables.Count == 0) ||
            (ds.Tables.Count == 1 && ds.Tables[0].Rows.Count == 0))
        {
        }
        else
        {
            bigList.DataSource = ds;
            bigList.DataBind();
        }
    }

    public void DataBindSmall()
    {
        try
        {
            DataSet ds = new DataSet();
            ds = bookBLL.SelectSmallType();
            if ((ds == null) ||
               (ds.Tables.Count == 0) ||
               (ds.Tables.Count == 1 && ds.Tables[0].Rows.Count == 0))
            {
            }
            else
            {
                smallList.DataSource = ds;
                smallList.DataBind();
            }
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('" + ex.Message.ToString() + "')</script>");
        }
    }

    public void DataBindDropList()
    {
        try
        {
            DataSet ds = bookBLL.SelectBigType();
            if ((ds == null) ||
                (ds.Tables.Count == 0) ||
                (ds.Tables.Count == 1 && ds.Tables[0].Rows.Count == 0))
            {
                return;
            }
            else
            {
                this.dropBig.DataSource = ds;
                this.dropBig.DataTextField = "BigType_Namestr";
                this.dropBig.DataBind();
            }
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('" + ex.Message.ToString() + "')</script>");
        }
    }
    #endregion

    #region GridView事件
    // 添加大类
    protected void Unnamed1_Click(object sender, EventArgs e)
    {
        this.bigPanel.Visible = true;    
    }
    // 添加小类
    protected void Button1_Click(object sender, EventArgs e)
    {
        this.smallPanel.Visible = true;    
    }
    // 大类修改
    protected void bigList_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "updateBig")
            {
                this.bigPanel.Visible = true;
                this.bigUpdate.Visible = true;
                this.bigAdd.Visible = false;

                GridViewRow drv = ((GridViewRow)(((LinkButton)(e.CommandSource)).Parent.Parent)); //此得出的值是表示那行被选中的索引值 (从0开始）   
                int bigId = Int32.Parse(this.bigList.Rows[drv.RowIndex].Cells[0].Text);           //取得是选中行的第一列索引 
                String bigName = this.bigList.Rows[drv.RowIndex].Cells[1].Text;                   //取得是选中行的第二列索引 
                this.bigTypeName.Text = bigName;
                this.bigTypeId.Text = bigId.ToString();
            }
            else // 删除
            {
                if (bookBLL.DeleteBig(Int32.Parse(e.CommandArgument.ToString())))
                { DataBindBig(); }
                else
                {
                    Response.Write("<script>alter('出现错误，请检查！')</script>");
                }
            }
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('" + ex.Message.ToString() + "')</script>");
        }
    }
    // 小类修改
    protected void smallList_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "updateSmall")
            {
                this.smallPanel.Visible = true;
                this.smallUpdate.Visible = true;
                this.smallAdd.Visible = false;

                GridViewRow drv = ((GridViewRow)(((LinkButton)(e.CommandSource)).Parent.Parent)); //此得出的值是表示那行被选中的索引值 (从0开始）   
                int smallId = Int32.Parse(this.smallList.Rows[drv.RowIndex].Cells[0].Text);           //取得是选中行的第一列索引 
                String smallName = this.smallList.Rows[drv.RowIndex].Cells[1].Text;                   //取得是选中行的第二列索引 
                String bigName = this.smallList.Rows[drv.RowIndex].Cells[2].Text;

                this.smallTypeId.Text = smallId.ToString();
                this.smallTypeName.Text = smallName;
                this.dropBig.SelectedItem.Text = bigName;
            }
            else  // 删除
            {
                if (bookBLL.DeleteSmall(Int32.Parse(e.CommandArgument.ToString())))
                { DataBindSmall(); }
                else
                {
                    Response.Write("<script>alter('出现错误，请检查！')</script>");
                }
            }
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('" + ex.Message.ToString() + "')</script>");
        }
    }
    #endregion

    #region 按钮事件
    // 大类更新
    protected void bigUpdate_Click(object sender, EventArgs e)
    {
        try
        {
            int bigTypeId = Int32.Parse(this.bigTypeId.Text.ToString());
            String bigTypeName = this.bigTypeName.Text.ToString();

            if (bookBLL.UpdateBig(bigTypeId, bigTypeName))
            { 
                DataBindBig();
            }
            else
            { 
                Response.Write("<script>alter('出现错误，请检查！')</script>"); 
            }
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('" + ex.Message.ToString() + "')</script>");
        }
    }
    // 大类添加
    protected void bigAdd_Click(object sender, EventArgs e)
    {
        try
        {
            String bigTypeName = this.bigTypeName.Text.ToString();
            if (bookBLL.InsertBig(bigTypeName))
            {
                DataBindBig(); 
            }
            else
            { 
                Response.Write("<script>alter('出现错误，请检查！')</script>"); 
            }
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('" + ex.Message.ToString() + "')</script>");
        }
    }
    // 小类更新
    protected void smallAdd_Click(object sender, EventArgs e)
    {
        try
        {
            String smallTypeName = this.smallTypeName.Text.ToString();
            String bigTypeName = this.dropBig.SelectedItem.Text.ToString();
            int smallTypeId = Int32.Parse(this.smallTypeId.Text.ToString());

            if (bookBLL.UpdateSmall(smallTypeId, bigTypeName, smallTypeName))
            { 
                DataBindSmall();
            }
            else
            { 
                Response.Write("<script>alter('出现错误，请检查！')</script>");
            }
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('" + ex.Message.ToString() + "')</script>");
        }
    }
    // 小类添加
    protected void smallUpdate_Click(object sender, EventArgs e)
    {
        try
        {
            String smallTypeName = this.smallTypeName.Text.ToString();
            String bigTypeName = this.dropBig.SelectedItem.Text.ToString();
            if (bookBLL.InsertSmall(bigTypeName, smallTypeName))
            { 
                DataBindSmall(); 
            }
            else
            { 
                Response.Write("<script>alter('出现错误，请检查！')</script>");
            }
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('" + ex.Message.ToString() + "')</script>");
        }
    }
    #endregion
}
